<?php 
/**
* 商品管理控制器
*/
class GoodsController extends AuthController{
    private $model;

     // 初始化函数
    public function __init(){
        parent::__init();
        $this->model = K('Goods');
    }

	// 首页 
    public function index(){
        $data = $this->model->where('is_show=1')->all();
        $this->assign('data',$data);
		$this->display();
	}

    // 已下架商品
    public function down(){
        $data = $this->model->where('is_show=0')->all();
        $this->assign('data',$data);
        $this->display();
    }

    // 编辑商品
    public function edit(){
        $gid = Q('get.gid',0,'intval');
        // 获得旧数据
        $oldGood = $this->model->where("gid={$gid}")->find();
        // p($oldGood);
        $this->assign('oldGood',$oldGood);

        // 获得商品详细的旧数据
        $oldDetail = K('GoodsDetail')->where("goods_gid={$gid}")->find();  
        $oldDetail['small'] = explode(',', $oldDetail['small_pic']);
        $oldDetail['mid'] = explode(',', $oldDetail['mid_pic']);
        $oldDetail['big'] = explode(',', $oldDetail['big_pic']);
        foreach ($oldDetail['small'] as $k => $v) {
            $img[$k][]=$v;
            $img[$k][]=$oldDetail['mid'][$k];
            $img[$k][]=$oldDetail['big'][$k];
        }
        // p($oldDetail);
        // p($img);
        $this->assign('img',$img);
        $this->assign('thumb',json_encode($img));
        $this->assign('oldDetail',$oldDetail);

        // 获得所属分类
        $cateData = K('Category')->all();
        $cateData = Data::tree($cateData,'cname');
        $this->assign('cateData',$cateData);

        // 获得所属品牌
        $brandsData = K('Brands')->all();
        $this->assign('brandsData',$brandsData);

        //获得所属分类的属性
        $attr=M()->join('__category__ c JOIN __type_attr__ ta ON c.type_tid=ta.type_tid JOIN __goods_attr__ ga ON ga.type_attr_taid=ta.taid')->where("goods_gid = {$oldGood['gid']} AND cid={$oldGood['category_cid']}")->all();
        foreach ($attr as $k => $v) {
            $attr[$k]['tavalue'] = explode(',', $v['tavalue']);
        }
        // p($attr);
        $this->assign('attr',$attr);

        if(IS_POST){
            // p($_POST);die;
            $this->model->where("gid={$gid}")->update();

            // // 添加商品详细表
            $thumb = $_POST['thumb'];

            $temp_big = array();
            $temp_mid = array();
            $temp_small = array();
            foreach ($thumb as $k => $v) {
                if(strstr($v, 'big')){
                    $temp_big[] = $v;
                }
                if(strstr($v, 'medil')){
                    $temp_mid[] = $v;
                }
                if(strstr($v, 'small')){
                    $temp_small[] = $v;
                }
            }
            $dataDetail = array(
                'detail'=>$_POST['detail'],
                'service'=>$_POST['service'],
                'use_method'=>$_POST['use_method'],
                'rel_photo'=>$_POST['rel_photo'],
                'goods_gid'=>$gid,
                'small_pic'=>implode(',', $temp_small),
                'mid_pic'=>implode(',', $temp_mid),
                'big_pic'=>implode(',', $temp_big),
            );
            // p($dataDetail);die;
            K('GoodsDetail')->where("goods_gid={$gid}")->update($dataDetail);
            // p(111);die;
            // 先把原来的属性删除再重新添加
            K('GoodsAttr')->where("goods_gid={$gid}")->delete();
            // 添加商品属性表
            if(Q('post.attr')){
                foreach(Q('post.attr') as $k=>$v){
                    if($v!='0'){
                        $dataAttr = array(
                            'attr'=> $v,     
                            'type_attr_taid'=> $k,
                            'extra_price' => 0,
                            'goods_gid'=>$gid,
                        );  
                        K('GoodsAttr')->add($dataAttr);
                    }  
                }
            }
            
            // p($_POST['spec']);die;
            if($_POST['spec']){
                foreach ($_POST['spec'] as $taid => $v) {
                    foreach ($v['tavalue'] as $key => $value) {
                        $data = array(
                            'attr' => $value,
                            'extra_price' => $v['added'][$key],
                            'type_attr_taid' => $taid,
                            'goods_gid'=>$gid,
                        );
                        K('GoodsAttr')->add($data);
                    }
                }
            }
            

            // 将货品列表中该商品的货品全部删除，前台会提示用户重新添加货品
            K('GoodsList')->where("goods_gid={$gid}")->delete();
            
             // p(Q('post.'));
            $this->success('修改成功',U('index'));
        }

        $this->display();
    }

	// 添加商品
	public function add(){

         // 把所有分类循环出来
        $cateData = K('Category')->all();
        $cateData = Data::tree($cateData,'cname');
        $this->assign('cateData',$cateData);

        // 把所有品牌循环出来
        $brandsData = K('Brands')->all();
        $this->assign('brandsData',$brandsData);
        if(IS_POST){
            // p(Q('post.'));die;
            //调用模型的添加，里面包含自动验证
            if(!$gid = $this->model->addData()){
                $this->error($this->model->error);
            }
            
            $dataGoods = array(
                'gname'=>Q('post.gname'),
                'unit'=>Q('post.unit'),
                'market_price'=>Q('post.market_price'),
                'mall_price'=>Q('post.mall_price'),
                'figure'=>Q('post.figure'),
                'click_num'=>Q('post.click_num'),
                'brands_bid'=>Q('post.brands_bid'),
                'category_cid'=>Q('post.category_cid'),
            );

            // 添加商品详细表
            $thumb = Q('post.thumb');
            $temp_big = array();
            $temp_mid = array();
            $temp_small = array();
            foreach ($thumb as $k => $v) {
                if($k%3==0){
                    $temp_big[] = $v;
                }
                if($k%3==1){
                    $temp_mid[] = $v;
                }
                if($k%3==2){
                    $temp_small[] = $v;
                }
            }
            $dataDetail = array(
                'detail'=>$_POST['detail'],
                'service'=>$_POST['service'],
                'use_method'=>$_POST['use_method'],
                'rel_photo'=>$_POST['rel_photo'],
                'goods_gid'=>$gid,
                'small_pic'=>implode(',', $temp_small),
                'mid_pic'=>implode(',', $temp_mid),
                'big_pic'=>implode(',', $temp_big),
            );
            K('GoodsDetail')->add($dataDetail);

            // 添加商品属性表
           foreach(Q('post.attr') as $k=>$v){
                if($v!='0'){
                    $dataAttr = array(
                        'attr'=> $v,     
                        'type_attr_taid'=> $k,
                        'extra_price' => 0,
                        'goods_gid'=>$gid,
                    );  
                    K('GoodsAttr')->add($dataAttr);
                }  
            }
                    
            foreach ($_POST['spec'] as $taid => $value) {
                foreach ($value['tavalue'] as $key => $v) {
                    $data = array(
                        'attr' => $v,
                        'extra_price' => $value['added'][$key],
                        'type_attr_taid' => $taid,
                        'goods_gid'=>$gid,
                    );
                    K('GoodsAttr')->add($data);
                }
            }

             // p(Q('post.'));
            $this->success('添加成功',U('index'));
        }
		$this->display();
	}

	// 上传文件
    public function upload(){
        $upload = new Upload('Upload/Content/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        }else{
            $data = $file[0];
            $this->ajax($data);
        }
    }

    public function upload_thumb()
    {
         $upload = new Upload('Upload/Content/' . date('y/m'));
         $file = $upload->upload();
         if (empty($file)) 
         {
            $this->ajax('上传失败');
         } else {
            //上传缩略处理
            $data = $file[0]['path'];
            $img = new Image();
            //重组文件名
            $big = $file[0]['dir'] . 'big' . $file[0]['filename'] . '.'.$file[0]['ext'];
            $mid = $file[0]['dir'] . 'medil' . $file[0]['filename'] . '.'.$file[0]['ext'];
            $small = $file[0]['dir'] . 'small' . $file[0]['filename'] . '.'.$file[0]['ext'];
            //这里要修改一下框架里的缩略图类
            $pathBig = $img->thumb($data,$big,800,800,4);
            $pathMid = $img->thumb($data,$mid,350,350,4);
            $pathSmall = $img->thumb($data,$small,80,80,4);
            $data = array(
                'big' => $pathBig,
                'mid' => $pathMid,
                'small' => $pathSmall
            );
            $this->ajax($data);
         }
    }


    // 异步删除图片
    public function del_pic(){
        $path = Q('post.path');
        // 删除图片
        unlink($path);
    }

    // 异步获得属性和规格
    public function getAttrSpec(){
        if(!IS_AJAX) $this->error('非法请求');
        $tid = Q('post.tid',0,'intval');
        // 通过tid获得相应的属性
        $data = K('Type_attr')->where("type_tid={$tid}")->all();
        // p($data);
        foreach ($data as $k => $v) {
            $data[$k]['tavalue'] = explode(',', $v['tavalue']);
        }
        // p($data);
        $this->ajax($data);
    }

    // 删除商品
    public function del(){
        $gid = Q('get.gid',0,'intval');

        $data = array('is_show'=>0);
        // 删除商品表
        $this->model->where("gid={$gid}")->update($data);

        // 删除商品详细表
        // K('GoodsDetail')->where("goods_gid={$gid}")->delete();

        // 删除商品属性表
        // K('GoodsAttr')->where("goods_gid={$gid}")->delete();

        $this->success('下架成功',U('index'));
    }

    // 商品重新上架
    public function up(){
        $gid = Q('get.gid',0,'intval');

        $data = array('is_show'=>1);
        // 更新商品表
        $this->model->where("gid={$gid}")->update($data);
        $this->success('上架成功',U('index'));
    }

    // 删除已经下架的商品
    public function reldel(){
        $gid = Q('get.gid',0,'intval');

        // 删除商品表
        $this->model->where("gid={$gid}")->delete();

        // 删除商品详细表
        K('GoodsDetail')->where("goods_gid={$gid}")->delete();

        // 删除商品属性表
        K('GoodsAttr')->where("goods_gid={$gid}")->delete();

        // 删除该商品下的所有货品
        K('GoodsList')->where("goods_gid={$gid}")->delete();
	        $this->success('已删除该商品',U('down'));
    }

}
 ?>